<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<% String context = request.getContextPath(); %>
<html>
<head>
    <title>业务流程</title>
    <link rel="stylesheet" href="<%=context %>/Source/lib/layui/css/layui.css">
</head>

<body style="padding: 0;overflow:hidden;">
<input id="type" type="hidden" value="${type}"/>
<form id="searchForm" onsubmit="return false">
    <div id="layout1" style="margin: -1px">
        <div position="center">
            <div id="toolbar" style="margin: 10px 0 0 10px">
                <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="f_add()">
                    <i class="layui-icon">&#xe654;</i> 增加
                </button>
            </div>
            <div id="mainGrid"></div>
        </div>
    </div>
</form>
</body>

<script src="<%=context %>/gooflow/jquery.min.js"></script>
<script src="<%=context %>/microcore/commonjs/utility.js"></script>
<script src="<%=context %>/Source/lib/layui/layui.js"></script>
<script type="text/javascript">
    var request_url = "/list/json?";
    var path = '<%=context %>';
    var _path = path + "/flow/process";
    var appendType = $('#type').val();
    if (appendType.length > 0) {
        request_url += "type=" + appendType + "&rnd=" + Math.random();
    } else {
        request_url += "rnd=" + Math.random();
    }

    var table;
    var layer;
    layui.use(['table','layer'], function(){

        table = layui.table;
        layer = layui.layer;

        table.render({
            elem: '#mainGrid'
            , url: _path+request_url //数据接口
            // , page: true //开启分页
            , cols: [[ //表头
                // {field: 'id', title: 'id', align: 'center', sort: true}
                {field: 'processKey', title: '流程定义KEY', align: 'center', sort: true}
                , {field: 'processName', title: '流程名称', align: 'center', sort: true}
                , {field: 'version', title: '版本号', align: 'center', sort: true}
                , {field: 'createDateStr', title: '版本时间', align: 'center', sort: true}
                , {title: '流程状态', align: 'center', sort: true, templet: function (item) {
                        var result = "";
                        if (item.state == '0') result = '未部署';
                        else if (item.state == '1') result = '已部署';
                        else if (item.state == '2') result = '已暂停';
                        return result;
                    }
                }
                ,{title: '操作', align: 'center', templet: function (item) {
                        if (item.state == '1'){
                            var result = "<a class=\"layui-btn layui-btn-primary  layui-btn-xs\" href=\"javascript:void(0)\" onclick=\"f_stop('" + item.processKey + "')\">暂停</a>";
                        }else if (item.state == '2'){
                            var result = "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" href=\"javascript:void(0)\" onclick=\"f_run('" + item.processKey + "')\">激活</a>";
                        }else {
                            var result = "<a class=\"layui-btn layui-btn-primary layui-btn-xs\" href=\"javascript:void(0)\" onclick=\"f_deploy('" + item.id + "')\">部署</a>";
                        }
                        result += "<a class=\"layui-btn layui-btn-xs\" href=\"javascript:void(0)\" onclick=\"f_edit('" + item.id + "')\">编辑</a>";
                        result += "<a class=\"layui-btn layui-btn-xs layui-btn-danger\" href=\"javascript:void(0)\" onclick=\"f_delete('" + item.id + "')\">删除</a>";
                        result += "<a class=\"layui-btn layui-btn-xs\" href=\"javascript:void(0)\" onclick=\"f_edit_bpmn('" + item.id + "')\">编辑流程图</a>";

                        return result;
                    }
                }
            ]]
        });
    });

    // 更新列表
    function f_load() {
        layui.table.reload('mainGrid', {
            url: _path+request_url
        });
    }

    var organizationId = "";
    var organizationName = "";

    function onSelect(note) {
        // var manager = $("#mainGrid").ligerGetGridManager();
        manager.set('parms', {search_organizationId: note.data.id, rnd: Math.random()});
        manager.loadData();
        organizationId = note.data.id;
        organizationName = note.data.name;
    }

    function onCancelselect(note) {
        // var manager = $("#mainGrid").ligerGetGridManager();
        manager.set('parms', {rnd: Math.random()});
        manager.loadData();
        organizationId = "";
        organizationName = "";
    }

    //添加
    function f_add() {
        layer.open({
            type: 2,
            title: '添加流程',
            shadeClose: true,
            shade: 0.8,
            area: ['500px', '500px'],
            content:_path + '/toEdit', //iframe的url
            btn: ['确定', '取消'],
            yes: function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                var nodeJson = iframeWin.getData();//调用子页面的方法，得到子页面返回的ids
                nodeJson = JSON.parse(nodeJson);

                app.ajax({
                    url: path + '/flow/process/save',
                    data: nodeJson,
                    success: function (result) {
                        if (result.isError) {
                            layer.close(index);
                            layer.msg(result.message);
                        } else {
                            layer.close(index);
                            layer.msg(result.message);
                            f_load();
                        }
                    },
                })
            }
        });
    }

    //编辑
    function f_edit(id) {
        layer.open({
            type: 2,
            title: '修改流程',
            shadeClose: true,
            shade: 0.8,
            area: ['500px', '500px'],
            content:_path + '/toEdit?id=' + id, //iframe的url
            btn: ['确定', '取消'],
            yes: function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                var nodeJson = iframeWin.getData();//调用子页面的方法，得到子页面返回的ids
                nodeJson = JSON.parse(nodeJson);
                console.log(nodeJson)
                $.ajax({
                    url: path + '/flow/process/save',
                    type: 'post',
                    dataType: 'json',
                    data: nodeJson,
                    success: function (result) {
                        console.log(result)
                        if(result.isError){
                            layer.msg(result.message);
                        }else{
                            layer.close(index);
                            layer.msg(result.message);
                            f_load();
                        }
                    },
                    error: function () {
                        console.log("error");
                    }
                });
            }
        });
    }

    //删除
    function f_delete(id) {
        var ids = [];
        ids.push(id);
        layer.confirm('流程删除不能恢复，确定删除？', {
            btn: ['确定','取消'] //按钮
        }, function(index, layero){
            app.ajax({
                url: _path + "/delete",
                data: {ids: ids.toString(), rnd: Math.random()},
                success: function (result) {
                    layer.msg(result.message);
                    f_load();
                }
            })

        });
    }

    //部署
    function f_deploy(id) {
        $.ajax({
            url: _path + "/deploy",
            type: "post", dataType: 'json',
            data: {"id": id},
            success: function (result) {
                if (result.isError) {
                    layer.msg(result.message);
                } else {
                    layer.msg(result.message);
                    f_load();
                }
            },
            error: function () {
                layer.msg('部署失败！');
            }
        });
    }

    function f_stop(key) {
        $.ajax({
            url: _path + "/stop",
            type: "post", dataType: 'json',
            data: {"key": key},
            success: function (result) {
                if (result.isError) {
                    layer.msg("暂停失败");
                } else {
                    layer.msg("暂停成功");
                    f_load();
                }
            },
            error: function () {
                layer.msg('部署失败！');
            }
        });
    }

    function f_run(key) {
        $.ajax({
            url: _path + "/run",
            type: "post",
            dataType: 'json',
            data: {"key": key},
            success: function (result) {
                if (result.isError) {
                    layer.msg("激活失败");
                } else {
                    layer.msg("激活成功");
                    f_load();
                }
            },
            error: function () {
                layer.msg('部署失败！');
            }
        });
    }

    //编辑流程图
    function f_edit_bpmn(id) {
        window.open(_path + '/processEditBpmn?id=' + id);
        // layer.open({
        //     type: 2,
        //     title: '编辑流程图',
        //     shadeClose: true,
        //     shade: 0.8,
        //     area: ['80%', '80%'],
        //     content:_path + '/processEditBpmn?id=' + id, //iframe的url
        //     btn: ['保存', '另存为新版本','清空'],
        // yes: function(index, layero){
        //     var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
        //     var nodeJson = iframeWin.getData();//调用子页面的方法，得到子页面返回的ids
        //     nodeJson = JSON.parse(nodeJson);
        //     // console.log(nodeJson)
        //     $.ajax({
        //         url: path + '/flow/process/save',
        //         type: 'post',
        //         dataType: 'json',
        //         data: nodeJson,
        //         success: function (result) {
        //             console.log(result);
        //             if (result.isError) {
        //                 layer.close(index);
        //                 layer.msg(result.message);
        //             } else {
        //                 layer.close(index);
        //                 layer.msg(result.message);
        //                 f_load();
        //             }
        //         },
        //         error: function () {
        //             console.log("error");
        //         }
        //     });
        // }
        // });
    }

</script>
</html>